package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.GetMovieDetailModel;
import com.smalls.newvideotwo.mvp.view.IGetMovieDetailView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

/**
 * Created by hs on 2018/3/20.
 */

public class GetMovieDetailPresenter extends BasePresenter<IGetMovieDetailView> implements BaseBeanResult.IGetMovieDetail {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetHotMoviePresenter":"GetHotMoviePresenter";
    private boolean isLive;
    private String mMovieId = null;
    private String mCategoryId = null;
    private com.smalls.newvideotwo.mvp.model.modelInterface.IGetMovieDetail model = new GetMovieDetailModel();
    public GetMovieDetailPresenter(IGetMovieDetailView view) {
        super(view);
    }

    public void request(String movie_id,String category_id,boolean z) {
        mMovieId = movie_id;
        mCategoryId = category_id;
        model.getMovieDetail(movie_id,category_id,this);
        isLive = z;
    }

    public void Abort() {
        if(model!=null) {
            model.Abort();
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");
            errCode = PortalErr.STR_BOX_EPG_TIMEOUT_ERROR;

            if (!NewDTVApp.getApp().isLastServer()) {
                NewDTVApp.getApp().setmEpgServerNext();
                if(mMovieId!=null) {
                    request(mMovieId, mCategoryId,false);
                }
                return ;
            }
        } else {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PortalErr.PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((IGetMovieDetailView) this.view).showErrDialogGetMovieDetail(errCode);
    }

    @Override
    public void onNext(MovieDetail result) {
        PortalErrUtil.getInstance().setErrCode("");
        if (result == null) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PortalErr.PORTAL_ROOT_COLUMN);
            ((IGetMovieDetailView) this.view).showErrDialogGetMovieDetail(PortalErr.PORTAL_ROOT_COLUMN);
        } else {
            LogUtil.d(this.TAG, this.TAG + "  onNext result=" + result.toString());
            if (this.isLive ) {
                LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fED1");
                PortalErrUtil.getInstance().setErrCode(PortalErr.STR_PRODUCT_WITHOUT_AUTH);
                ((IGetMovieDetailView) this.view).showErrDialogGetMovieDetail(PortalErr.STR_PRODUCT_WITHOUT_AUTH);
            } else {
                ((IGetMovieDetailView) this.view).onSucceedGetMovieDetail(result);
            }
        }
    }

}
